#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

vector<int> sortByBits(vector<int>& arr) {
    auto proj = [](int x) {
        return pair<int,int>{ __builtin_popcount(x), x };
    };
    sort(begin(arr), end(arr),
        [&](int a, int b) { return proj(a) < proj(b); });
    return arr;
}